package com.etone.smartAudit.service;

import cn.hutool.core.util.StrUtil;
import com.etone.smartAudit.domain.Admin;
import com.etone.smartAudit.domain.CertificateReply;
import com.etone.smartAudit.dto.certificate.CertificateReplyDTO;
import com.etone.smartAudit.error.CustomParameterizedException;
import com.etone.smartAudit.mapper.AdminMapper;
import com.etone.smartAudit.mapper.CertificateReplyMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etone.smartAudit.security.SecurityUtils;
import com.etone.smartAudit.vo.certificate.CertificateReplyVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 回复意见  服务实现类
 * </p>
 *
 * @author jiayen
 * @since 2021-06-23
 */
@Service
public class CertificateReplyService extends ServiceImpl<CertificateReplyMapper, CertificateReply> {
    @Autowired
    private AdminMapper adminMapper;

    /***
     * 添加回复评论
     * @param dto
     */
    public void addReply( CertificateReplyDTO dto) {
        String loginAdminName = SecurityUtils.getCurrentUserLogin();
        Admin admin = adminMapper.findByAccount(loginAdminName);
        CertificateReply reply = new CertificateReply();
        BeanUtils.copyProperties(dto, reply);
        if (admin != null) {
            reply.setPersonId(admin.getId());
            reply.setType(CertificateReply.TYPE_ADMIN);
        } else {
            reply.setType(CertificateReply.TYPE_HAND);
            if (dto.getHandId() == null) {
                throw new CustomParameterizedException("执行人信息不能为空");
            }
            reply.setPersonId(dto.getHandId());
        }
        reply.setCreateTime(LocalDateTime.now());
        this.save(reply);
    }

    /**
     * 获取回复列表
     *
     * @param handId
     * @return
     */
    public List<CertificateReplyVO> getReplyList(Integer handId) {
        List<CertificateReplyVO> list = baseMapper.getReplyList(handId);
        list.stream().forEach(vo -> {
            if (StrUtil.isEmpty(vo.getPersonName())) {
                vo.setPersonName(vo.getAdminName());
            }
        });
        return list;
    }
    /**
     * 获取回复列表
     *
     * @param certifiFileId
     * @return
     */
    public List<CertificateReplyVO> getReplyListByCertiFileId(Integer certifiFileId) {
        if(certifiFileId==null){
            return new ArrayList<>();
        }
        List<CertificateReplyVO> list = baseMapper.getReplyListByCertiFileId(certifiFileId);

        return list;
    }
}
